package util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Usuario;

public class FiltroLogin implements Filter {

	public FiltroLogin() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		chain.doFilter(request, response);
		
//		HttpServletRequest httpRequest = (HttpServletRequest) request;
//		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//		Usuario usuario = (Usuario) httpRequest.getSession().getAttribute("usuarioLogado");
//		String url = httpRequest.getRequestURI();
//		if (usuario == null) {
//			if (url.contains("/faces/resources/login/login.xhtml")) {
//				chain.doFilter(request, response);
//			} else {
//				httpServletResponse.sendRedirect("/faces/resources/login/login.xhtml");
//			}
//		} else {
//			String tipoUsuario = usuario.getGrupoUsuario().getNome();
//			
//			if(tipoUsuario.equals("Empresa")){
//				if(url.contains("/faces/resources/empresa/empresa.xhml") ||
//						url.contains("/faces/resources/indicacao/indicacaoListagem.xhml") ||
//						url.contains("/faces/resources/vaga/vaga.xhml"))
//					chain.doFilter(request, response);
//				else
//					httpServletResponse.sendRedirect("/faces/resources/login/permissaoNegada.xhtml");
//			}else if(tipoUsuario.equals("Aluno")){
//				if(url.contains("/faces/resources/curriculo/curriculo.xhtml"))
//					chain.doFilter(request, response);
//				else
//					httpServletResponse.sendRedirect("/faces/resources/login/permissaoNegada.xhtml");
//			}else if(tipoUsuario.equals("Professor")){
//				if(url.contains("/faces/resources/indicacao/indicacao.xhtml") ||
//						url.contains("/faces/resources/indicacao/indicacaoAluno.xhtml") ||
//						url.contains("/faces/resources/indicacao/professorIndicacao.xhtml"))
//					chain.doFilter(request, response);
//				else
//					httpServletResponse.sendRedirect("/faces/resources/login/permissaoNegada.xhtml");
//			}else if(tipoUsuario.equals("Administrador")){
//				chain.doFilter(request, response);
//			}else{
//				httpRequest.getSession().removeAttribute("usuarioLogado");
//				httpServletResponse.sendRedirect("/faces/resources/login/login.xhtml");
//			}
//		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
